// pages/indexpage/indexpage.js
const app = getApp(), core = require('../../utils/core'),chatIM = require('../../utils/websocket.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentCity: null,
    banners:[],
    current: 0,
    newsBanners: [],
    text_current:0,
    jobs: [],
    companies: [],
    backTopValue: false
  },
  onPageScroll: function (e) {
    var that = this
    var scrollTop = e.scrollTop
    var backTopValue = scrollTop > 500 ? true : false
    that.setData({
      backTopValue: backTopValue
    })
  },
  // 滚动到顶部
  backTop: function () {
    // 控制滚动
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  swiperchange:function(e){
    this.setData({
      current:e.detail.current
    })
  },
  swiperTextchange: function (e) {
    this.setData({
      text_current: e.detail.current
    })
  },
  onLoad: function (options) {
    let page = this;
    let userinfo = app.getUserinfo();
    if(userinfo && (userinfo.group_id==2||userinfo.group_id==3) && (userinfo.group_model.audit_status==0||userinfo.group_model.audit_status==3)) {
      //企业账号未审核，跳到审核页面
      wx.reLaunch({url:'/pages/enterprise_audit/enterprise_audit'});
      return;
    }
    this.setData({currentCity: app.getCurrentCity()});
    this.refresh();
	app.eventBus.on(app.Events.CurrentCityChange, this.eventOnCurrentCityChange, this);
    app.eventBus.on(app.Events.UserLogin, this.eventOnUserLogin, this);
    app.eventBus.on(app.Events.UserLogout, this.eventOnUserLogout, this);
	app.eventBus.on(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate, this);
	//未读消息
	if (app.globalData.chat_config.SocketTask) {
		chatIM.setMessageBadge(false)
	 }
	app.eventBus.on(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount,this);
    this.setData({
      isIPhoneX: app.globalData.isIPhoneX
    });
  },
  onUnload: function () {
    app.eventBus.off(app.Events.CurrentCityChange, this.eventOnCurrentCityChange);
    app.eventBus.off(app.Events.UserLogin, this.eventOnUserLogin);
    app.eventBus.off(app.Events.UserLogout, this.eventOnUserLogout);
	app.eventBus.off(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate);
	app.eventBus.off(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount);
  },
  onShow: function () {
    var that = this;
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      });
    }
  },
  refresh() {
    let page = this;
    let userinfo = app.getUserinfo();
    this.setData({jobs:[], resumes:[], companies:[], userinfo: userinfo});
    core.get({url: '/xilujob.news/announcement?pagesize=3',loading:false,success(ret,response){page.setData({newsBanners:ret.data.data})}});
    core.get({url:'/xilujob.banner/banners/group/index',loading:false,success(ret,response) {
      page.setData({banners: ret.data});
    }});
    this.fetchCompany();
    if(userinfo && userinfo.group_id!=1) {
      this.fetchResume();
    }else {
      this.fetchJob();
    }
    wx.stopPullDownRefresh();
  },
  fetchCompany() {
    core.get({url:'xilujob.company/lists',loading:false,success:ret=>{
      this.setData({companies: ret.data.data.slice(0,3)});
    }});
  },
  fetchJob() {
    core.get({url:'xilujob.job/recommend_jobs',loading:false,success:ret=>{
      this.setData({jobs: ret.data.jobs});
    }});
  },
  fetchResume() {
    core.get({url:'xilujob.resume/recommend_resumes',loading:false,success:ret=>{
      this.setData({resumes: ret.data.data});
    }});
  },
  bindGoLabourJob() {
    core.setCache('seek_job_type','labour',2);
    wx.switchTab({url:'/pages/seek_job/seek_job'});
  },
  bindGoParttimeJob() {
    core.setCache('seek_job_type','parttime',2);
    wx.switchTab({url:'/pages/seek_job/seek_job'});
  },
  bindGoRedpacketJob() {
    core.setCache('seek_job_type','redpacket',2);
    wx.switchTab({url:'/pages/seek_job/seek_job'});
  },
  bindGoHurryJob() {
    core.setCache('seek_job_type','hurry',1);
    wx.switchTab({url:'/pages/seek_job/seek_job'});
  },
  eventOnCurrentCityChange: function (currentCity) {
    this.setData({currentCity});
    this.refresh();
  },
  eventOnUserLogin(userinfo) {
    this.refresh();
  },
  eventOnUserLogout() {
    this.refresh();
  },
  eventOnUserinfoUpdate() {
    this.refresh();
  },
  eventWebsocketMessageCount(data){
	var list = this.getTabBar().data.list;
	list[1].megNum = app.globalData.chat_config.messageCount;
	this.getTabBar().setData({
	  list
	})
  },
  onPullDownRefresh: function () {
    this.refresh();
  },
  onShareAppMessage() {

  },
  load(e) {
    //console.log(e);
  },
  error(e) {
    //console.error(e);
  }
});
